S.Node.Instance = function (option) {
    let me = this;
    Q.doSuperConstructor(me, S.Node.Instance, [option.name]);
    me.setOption(me.option);
    me.setOption(option);
};

S.Node.Instance.prototype = {
    xtype: 'instance',
    option: {
        zIndex: 1,
        movable: false,
        selectable: true,
        size: {
            width: 24
        },
        style: {
            alpha: 1,
            label: {
                color: '#fff',
                size: {
                    width: 230,
                    height: 28
                },
                position: 'LEFT_MIDDLE',
                padding: '0 8 0 0',
                anchor: {
                    position: 'LEFT_MIDDLE'
                },
                font: {
                    size: 13
                }
            },
            padding: '0 8 0 0',
            border: {
                width: 0,
                radius: 0
            }
        }
    },

    setOption: function (option) {
        let me = this;
        S.setOption(me, option);
    },

    colors: function () {
        let styles = this.styles;
        return {
            font: styles['label.color'],
            border: styles['border.color'],
            background: styles['background.color']
        }
    },

    expander: function () {
        return this.findByXType('expander')[0];
    },

    frame: function () {
        return this.findByXType('frame')[0];
    },

    bottom: function () { // 只处理两级，不递归
        let me = this, result;
        for (let child of me.children.datas) {
            result = result ? (child.y > result.y ? child : result) : child;
            for (let child2 of child.children.datas) {
                result = child2.y > result.y ? child2 : result;
            }
        }
        return result;
    },

    findByXType: function (xtype) {
        let results = [], me = this;
        for (let child of me.children.datas) {
            if (child.xtype == xtype) {
                results.push(child);
            }
        }
        return results;
    },

    setData: function (data) {
        this.data = data;
    },

    onDblClick: function (evt) {
        let expander = this.findByXType('expander')[0];
        expander && expander.click();
    },

    existedEdge: function (to) {
        let me = this, edges = me.getEdges();
        for (let edge of edges) {
            if (edge.to.id == to) {
                return true;
            }
        }
    },

    childrenEdgeFroms: function (children, froms) {
        let me = this;
        froms = froms || [];
        children = children || me.children.datas;
        for (let child of children) {
            if (child.edgeCount) {
                let edges = child.getEdges();
                for (let edge of edges) {
                    if (child.id == edge.to.id) {
                        !me.containInstance(froms, edge.from) && froms.push(edge.from);
                    }
                }
            }
            if (child.childrenCount > 0) {
                me.childrenEdgeFroms(child.children.datas, froms);
            }
        }
        return froms;
    },

    containInstance: function (instances, instance) {
        for (let e of instances) {
            if (e.data.id == instance.data.id) {
                return true;
            }
        }
    },

    removeEdgesNotMine: function () {
        let me = this, froms = [];
        if (me.edgeCount) {
            let edges = me.getEdges(), gm = me.graph.graphModel;
            for (let edge of edges) {
                if (me.id == edge.from.id) {
                    if (edge.fromId != edge.from.data.id) {
                        gm.remove(edge);
                    }
                } else if (me.id == edge.to.id) {
                    froms.push(edge.from);
                    if (edge.toId != edge.to.data.id) {
                        gm.remove(edge);
                    }
                }
            }
        }
        return froms;
    },

    removeChildren: function () {
        let me = this, gm = me.graph.graphModel, count = me.childrenCount;
        for (let i = count - 1; i >= 0; i--) {
            let child = me.children.datas[i];
            if (child.xtype != 'expander') {
                gm.remove(child);
            }
        }
    },

    removeFrame: function () {
        let me = this, gm = me.graph.graphModel;
        for (let child of me.children.datas) {
            if (child.xtype == 'frame') {
                gm.remove(child);
            }
        }
    }

};

Q.extend(S.Node.Instance, Q.Node);

